﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcfQQ.model;
namespace WcfQQ
{
    public class QQDao
    {
        private static readonly string connectionString = "server= localhost ;database=learn;uid=sa;pwd=123456;";
        //添加
        public static bool add(model.QQ qq)
        {
            string sql = string.Format("INSERT INTO QQ(name,pwd) values('{0}','{1}')", qq.name, qq.pwd);
            int result = SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql);
            return result > 0 ? true : false;
        }
        //删除
        public static bool del(int id)
        {
            string sql = string.Format("DELETE QQ WHERE ID = {0}", id);
            int result = SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql);
            return result > 0 ? true : false;
        }
        //获取单个
        public static QQ getOne(int id)
        {
            QQ qq = new QQ();
            string sql = string.Format("SELECT * FROM QQ WHERE ID = {0}", id);
            DataSet ds = SqlHelper.ExecuteDataset(connectionString, CommandType.Text, sql);
            if(ds != null && ds.Tables.Count > 0)
            {
                foreach(DataRow dr in ds.Tables[0].Rows)
                {
                    qq.id = Convert.ToInt32(dr["id"]);
                    qq.name = dr["name"].ToString();
                    qq.pwd = dr["pwd"].ToString();
                }
            }
            return qq;
        }
        //获取所有
        public static List<QQ> getAll()
        {
            List<QQ> list = new List<QQ>();
            string sql = string.Format("select * from qq");
            DataSet ds = SqlHelper.ExecuteDataset(connectionString, CommandType.Text, sql);
            if(ds != null && ds.Tables.Count > 0)
            {
                foreach (DataTable dt in ds.Tables)
                {
                    foreach(DataRow dr in dt.Rows)
                    {
                        QQ qq = new QQ();
                        qq.id = Convert.ToInt32(dr["id"]);
                        qq.name = dr["name"].ToString();
                        qq.pwd = dr["pwd"].ToString();
                        list.Add(qq);
                    }
                }
            }
            return list;

        }
    }
}